# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.AuthTokenOperation import authTokenOperation
from . import app

@app.route('/generateToken',methods=['POST'])
def generateToken():
    if not request.json:
        abort(400)
    try:
        phone = request.json['userPhone']
        password = request.json['userPassword']
    except:
        return jsonify({"message": {"code": 0, "info": "需要参数userPhone,userPassword！"}})

    code2, token = authTokenOperation().generateToken(phone, password)

    if code2 == 0:
        return jsonify({"message": {"code": 0, "info": "用户不存在！"}})
    elif code2 == -1:
        return jsonify({"message": {"code": -1, "info": "密码错误！"}})
    else:
        return jsonify({"token": token, "message": {"code": 1, "info": "生成token成功！"}})


@app.route('/checkToken', methods=['POST'])
def checkToken():
    """
    从 if not 到if checkResult:为每个api调用前的代码，用于验证token，只有token验证通过，才允许实际执行，否则直接转到
    loginUrl = "http://121.40.183.170:5005/zzh/app/v2/generateToken" 重新进行验证
        return jsonify({"messge": "token验证失败，请重新前往用户名和密码验证链接", "loginUrl": loginUrl})



    """
    if not request.json:
        abort(400)
    try:
       token = request.headers.get('Access-Token')
    except:
        return jsonify({"message": {"code": 0, "info": "需要参数   Access-Token"}})

    checkResult = authTokenOperation().veryfyToken(token)
    if checkResult:

        #这个地方添加实际的业务代码，每个api的原来的代码移动到这里面
        #同时把下方return jsonify({"message":"Token验证成功"})去掉


        return jsonify({"message":"Token验证成功"})
    else:
        loginUrl = "http://121.40.183.170:5005/zzh/app/v2/userLogin"
        return jsonify({"message": "token验证失败，请重新前往登录界面验证用户名和密码", "userLoginUrl": loginUrl})
